package com.ja.openchat.views;

import android.content.Context;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatEditText;

import com.ja.openchat.R;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.util.XPopupUtils;

import me.goldze.mvvmhabit.utils.ToastUtils;


public class CustomCirclePopup extends CenterPopupView {
    public CustomCirclePopup(@NonNull Context context) {
        super(context);
    }

    // 返回自定义弹窗的布局
    @Override
    protected int getImplLayoutId() {
        return R.layout.custom_circle_popup;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        AppCompatEditText et_money = findViewById(R.id.et_money);
        TextView tv_ok = findViewById(R.id.tv_ok);
        View ll_1 = findViewById(R.id.ll_1);
        View ll_2 = findViewById(R.id.ll_2);
        RadioGroup rg_1 = findViewById(R.id.rg_1);
        RadioGroup rg_2 = findViewById(R.id.rg_2);
        findViewById(R.id.iv_close).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss(); // 关闭弹窗
            }
        });
        findViewById(R.id.tv_fixed).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ll_1.setVisibility(GONE);
                ll_2.setVisibility(VISIBLE);
                tv_ok.setText("其他金额");
            }
        });
        tv_ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_ok.getText().toString().trim().equals("其他金额")) {
                    ll_1.setVisibility(VISIBLE);
                    ll_2.setVisibility(GONE);
                    tv_ok.setText("确定");
                } else {
                    //确定
                    ToastUtils.showShort("确定");
                    dismiss();
                }
            }
        });
        et_money.addTextChangedListener(new DigitTextWatcher1(et_money));
        rg_1.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId != -1 && group.getCheckedRadioButtonId() == checkedId) {
                if (((RadioButton) findViewById(checkedId)).isChecked()) {
                    rg_2.clearCheck();
                }
            }
        });
        rg_2.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId != -1 && group.getCheckedRadioButtonId() == checkedId) {
                if (((RadioButton) findViewById(checkedId)).isChecked()) {
                    rg_1.clearCheck();
                }
            }
        });
    }

    // 设置最大宽度，看需要而定，
    @Override
    protected int getMaxWidth() {
        return popupInfo.maxWidth == 0 ? (int) (XPopupUtils.getAppWidth(getContext()) * 0.91f)
                : popupInfo.maxWidth;
    }
}
